package tienda.zapateria.siszap.da;

import java.sql.Connection;
import tienda.zapateria.siszap.be.BETipo;
import java.util.ArrayList;
import org.rx.cr.dao.DAOAbstract;

public class DATipo extends DAOAbstract<BETipo> {

    public DATipo(Connection c) {
        setConnection(c);
    }

    @Override
    public int registrar(BETipo be) throws Exception {
        try {
            setStoreProcedure("zapateria.sp_ins_tipo");
            setParameterString(be.getNombre());
            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public int actualizarRegistro(BETipo be) throws Exception {
        try {
            setStoreProcedure("zapateria.sp_upd_tipo");
            setParameterInt(be.getTipoid());
            setParameterString(be.getNombre());
            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public int eliminarRegistro(BETipo be) throws Exception {
        try {
            setStoreProcedure("zapateria.sp_del_tipo");
            setParameterInt(be.getTipoid());
            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public ArrayList<BETipo> buscarReferencia(String string) throws Exception {
        try {
            clear();
            setStoreProcedure("zapateria.sp_sel_tipo");
            setParameterString(string);
            commitc();
            while (existResult()) {
                BETipo be = new BETipo();
                be.setTipoid(getDataInt(1));
                be.setNombre(getDataString(2));

                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public ArrayList<BETipo> listar() throws Exception {
        try {
            clear();
            setStoreProcedure("zapateria.sp_sel_tipo");
            setParameterString("");
            commitc();
            while (existResult()) {
                BETipo be = new BETipo();
                be.setTipoid(getDataInt(1));
                be.setNombre(getDataString(2));

                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public BETipo buscar(int i) throws Exception {
      try {
            BETipo be = null;
            clear();
            setStoreProcedure("zapateria.sp_sel_tipo_id");
            setParameterInt(i);
            commitc();
            while (existResult()) {
                be = new BETipo();
                 be.setTipoid(getDataInt(1));
                be.setNombre(getDataString(2));
                
            }
            return be;
        } catch (Exception e) {
            rollback(e);
            return null;
        }  }

}
